﻿#include "renmianruanjian.h"
#include "ui_renmianruanjian.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QDebug>
#include <QIODevice>
#include <QTextEdit>
#include <QFile>
#include <QTextStream>
#include <QTextCodec>
#include <QDomDocument>
#include <QSqlQuery>
#include <QStringList>
renmianruanjian::renmianruanjian(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::renmianruanjian)
{
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF8"));
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF8"));
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF8"));
    //ui->pushButton_7->setAttribute(Qt::WA_TransparentForMouseEvents, true);
    ui->setupUi(this);
    ui->table->setStyleSheet("selection-background-color:rgb(255,209,128)");
    ui->table->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->table->setEditTriggers(QAbstractItemView::NoEditTriggers);

    ui->rudang1->setDisplayFormat("yyyy-MM-dd");//设置输出格式 ; 开始时间格式为2000-01-01 样式
    ui->rudang1->setCalendarPopup(true);

    ui->joinwork1->setDisplayFormat("yyyy-MM-dd");//设置输出格式 ; 开始时间格式为2000-01-01 样式
    ui->joinwork1->setCalendarPopup(true);

    ui->birth1->setDisplayFormat("yyyy-MM-dd");//设置输出格式 ; 开始时间格式为2000-01-01 样式
    ui->birth1->setCalendarPopup(true);
    //代码表的数据不允许修改,时间也不许用line修改
    ui->sex->setFocusPolicy(Qt::NoFocus);
    ui->minzu->setFocusPolicy(Qt::NoFocus);
    ui->birth->setFocusPolicy(Qt::NoFocus);
    ui->joinwork->setFocusPolicy(Qt::NoFocus);
    ui->rudang->setFocusPolicy(Qt::NoFocus);
    ui->politic->setFocusPolicy(Qt::NoFocus);
    ui->jiguan->setFocusPolicy(Qt::NoFocus);
    //姓名和证件号是唯一认证，不允许修改
    ui->name->setFocusPolicy(Qt::NoFocus);
    ui->id->setFocusPolicy(Qt::NoFocus);

    //不点击数据无法导入
    ui->pushButton_2->setEnabled(false);
    ui->pushButton_4->setEnabled(false);
    ui->pushButton_5->setEnabled(false);
    ui->pushButton_3->setEnabled(false);

    ui->scrollAreaWidgetContents->setAttribute(Qt::WA_TransparentForMouseEvents, true);

    connect(ui->sheng,SIGNAL(currentIndexChanged(QString)),this,SLOT(printshi()));
    connect(ui->shi,SIGNAL(currentIndexChanged(QString)),this,SLOT(printqu()));
}

renmianruanjian::~renmianruanjian()
{
    delete ui;
}

void renmianruanjian::printqu(){//写入区信息
    ui->qu->clear();
    ui->qu->addItem("区");
    QString sheng=ui->sheng->currentText();
    int shengnum=sheng.length();
    QString shi=ui->shi->currentText();
    QString all=sheng+shi;
    QSqlQuery shengshi;
    QSqlQuery selectall;

    shengshi.exec(QString("select * from AdminAreaCode where AdminArea='%1'").arg(all));
    while(shengshi.next()){
        QString id=shengshi.value(0).toString();//xxxxoo
        selectall.exec("select * from AdminAreaCode");
        while(selectall.next()){
            QString allid=selectall.value(0).toString();
            QString allname=selectall.value(1).toString();
            int allnamenum=allname.length();
            if(allid.mid(0,4)==id.mid(0,4) && allid.mid(4,2) != "00"){
                ui->qu->addItem(allname.mid(shengnum,allnamenum-shengnum));
            }
        }
    }

}

void renmianruanjian::printshi(){//写入市信息
    ui->shi->clear();
    ui->shi->addItem("市");
    QString sheng=ui->sheng->currentText();
    int shengnamenum=sheng.length();
    QSqlQuery selectsheng;
    QSqlQuery selectall;
    selectsheng.exec(QString("select * from AdminAreaCode where AdminArea='%1'").arg(sheng));
    while(selectsheng.next()){
        QString shengid=selectsheng.value(0).toString();
        QString xxoooo=shengid.mid(0,2);//前两位
        selectall.exec("select * from AdminAreaCode");
        while(selectall.next()){
            QString all=selectall.value(0).toString();
            QString allname=selectall.value(1).toString();
            int shinamenum=allname.length();
            allname=allname.mid(shengnamenum,shinamenum-shengnamenum);
            if(all.mid(0,2)==xxoooo && all.mid(4,2)=="00" &&all.mid(2,2)!="00"){
                ui->shi->addItem(allname);
            }
        }
    }
}

void renmianruanjian::on_pushButton_clicked()//导入文件
{
    QSqlQuery query;
    QSqlQuery changepolitic;//转换政治面貌代码
    QSqlQuery changeunit;//转换单位代码到单位名称
    QSqlQuery changejiguan;
    QString fileName = QFileDialog :: getOpenFileName(this, NULL,NULL, "*.xml");//打开xml文件
    qDebug()<<fileName;
    QFile file(fileName);
    //    //--打开文件成功
    //    if (file.open(QFile ::ReadOnly | QFile ::Text))
    //    {
    //        //qDebug()<<"1";
    //        QTextStream textStream(&file);
    //        while (!textStream.atEnd())
    //        {
    //            //---QtextEdit按行显示文件内容
    //            ui->showText->append(textStream.readLine());
    //        }
    //    }
    //    else	//---打开文件失败
    //    {
    //        /*
    //            information函数参数说明：
    //            函数原型
    //            QMessageBox::information(
    //            QWidget * parent,
    //            const QString & title,					//--- 标题
    //            const QString & text,					//---显示内容
    //            StandardButtons buttons = Ok,	//---OK按钮
    //            StandardButton defaultButton = NoButton)
    //            [static]
    //            */
    //        QMessageBox ::information(NULL, NULL, "open file error");
    //    }

    //打开文件成功后的操作

    //    QDomElement docElem = qDomDoc.documentElement();
    //    QDomNode nodeswitch=docElem.elementsByTagName("item");//红色的为标签名
    //    QDomElement elemnodeswitch=nodeswitch.toElement();
    //    string lie=qPrintable(elemnodeswitch.attribute("lie"));
    //    if(lie=="id")
    //    {
    //    qDebug()<<"succes";
    //    }

    QDomDocument docXML;
    if (!file.open(QIODevice::ReadOnly))
    {
        return ;
    }
    if (!docXML.setContent(&file))
    {
        file.close();
        return;
    }
    file.close();

    QDomElement xmlRoot = docXML.documentElement(); //返回根节点，table节点
    QDomNode domNode = xmlRoot.firstChild(); //获得第一个子节点,hang节点
    //    QString Name = xmlRoot.tagName();
    //    qDebug()<<Name;
    while (!domNode.isNull())  //如果节点不空
    {
        //        QString tagName = domNode.toElement().tagName();  //节点元素名称，hang节点
        //        qDebug()<<tagName;
        int row=ui->table->rowCount();//当前table行数
        ui->table->insertRow(row);//增加一行table
        QDomElement domElement = domNode.toElement(); //转换为元素，注意元素和节点是两个数据结构，其实差不多
        //        QDomNodeList list = domElement.childNodes();
        //        for (int i = 0; i<list.count(); i++) //遍历子元素，count和size都可以用,可用于标签数计数
        //        {
        QDomNode node = domElement.firstChild();
        while (!node.isNull()) {
            if (node.toElement().tagName() == "name") {
                QString no=node.firstChild().toText().data();
                //                qDebug()<<no;
                ui->table->setItem(row,0,new QTableWidgetItem(no));
            }
            else if (node.toElement().tagName() == "birth") {
                QString no=node.firstChild().toText().data();
                //                qDebug()<<no;
                ui->table->setItem(row,1,new QTableWidgetItem(no));
            }
            else if (node.toElement().tagName() == "id") {
                QString no=node.firstChild().toText().data();
                //                qDebug()<<no;

                ui->table->setItem(row,2,new QTableWidgetItem(no));
            }
            else if (node.toElement().tagName() == "sex") {
                QString no=node.firstChild().toText().data();
                //                qDebug()<<no;
                if(no=="1"){
                    no="男";
                }else if(no=="0")
                {
                    no="女";
                }
                ui->table->setItem(row,3,new QTableWidgetItem(no));
            }
            else if (node.toElement().tagName() == "nuit") {
                QString no=node.firstChild().toText().data();
                changeunit.exec(QString("select unitname from unit where unitid ='%1'").arg(no));
                while(changeunit.next()){
                    no=changeunit.value(0).toString();
                }
                ui->table->setItem(row,4,new QTableWidgetItem(no));
            }
            else if (node.toElement().tagName() == "zhiwu") {
                QString no=node.firstChild().toText().data();
                ui->table->setItem(row,5,new QTableWidgetItem(no));
            }
            else if (node.toElement().tagName() == "minzu") {
                QString no=node.firstChild().toText().data();
                query.exec(QString("select Nation from NationCode where NationID ='%1'").arg(no));
                while(query.next())
                {
                    QString Nation=query.value(0).toString();
                    ui->table->setItem(row,6,new QTableWidgetItem(Nation));
                }
            }
            else if (node.toElement().tagName() == "rudang") {
                QString no=node.firstChild().toText().data();
                ui->table->setItem(row,7,new QTableWidgetItem(no));
            }
            else if (node.toElement().tagName() == "joinwork") {
                QString no=node.firstChild().toText().data();
                ui->table->setItem(row,8,new QTableWidgetItem(no));
            }
            else if (node.toElement().tagName() == "health") {
                QString no=node.firstChild().toText().data();
                ui->table->setItem(row,9,new QTableWidgetItem(no));
            }
            else if (node.toElement().tagName() == "jiguan") {
                QString no=node.firstChild().toText().data();
                changejiguan.exec(QString("select AdminArea from AdminAreaCode where AdminAreaID='%1'").arg(no));
                while(changejiguan.next()){
                    no=changejiguan.value(0).toString();
                }

                ui->table->setItem(row,10,new QTableWidgetItem(no));
            }
            else if (node.toElement().tagName() == "politic") {//政治面貌代码表转换
                QString no=node.firstChild().toText().data();
                changepolitic.exec(QString("select Politic from PoliticCode where PoliticID='%1'").arg(no));
                while(changepolitic.next()){
                    no=changepolitic.value(0).toString();
                }
                ui->table->setItem(row,11,new QTableWidgetItem(no));
            }
            else if (node.toElement().tagName() == "schoolmajor") {
                QString no=node.firstChild().toText().data();
                ui->table->setItem(row,12,new QTableWidgetItem(no));
            }
            else if (node.toElement().tagName() == "workmajor") {
                QString no=node.firstChild().toText().data();
                ui->table->setItem(row,13,new QTableWidgetItem(no));
            }
            else if (node.toElement().tagName() == "schoolname") {
                QString no=node.firstChild().toText().data();
                ui->table->setItem(row,14,new QTableWidgetItem(no));
            }
            else if (node.toElement().tagName() == "workname") {
                QString no=node.firstChild().toText().data();
                ui->table->setItem(row,15,new QTableWidgetItem(no));
            }



            node=node.nextSibling();
        }
        //        }
        //下一个兄弟节点,nextSiblingElement()是下一个兄弟元素
        domNode = domNode.nextSibling();
    }
    file.close();
    ui->pushButton_3->setEnabled(true);
}

void renmianruanjian::on_pushButton_4_clicked()//选中数据显示数据，并进行修改
{
    //combo
    //    ui->sex1->clear();
    ui->scrollAreaWidgetContents->setAttribute(Qt::WA_TransparentForMouseEvents, false);
    ui->name->clear();
    ui->id->clear();
    ui->unit->clear();
    ui->birth->clear();
    ui->sex->clear();
    ui->zhiwu->clear();
    ui->minzu->clear();
    ui->rudang->clear();
    ui->health->clear();
    ui->jiguan->clear();
    ui->joinwork->clear();
    ui->politic->clear();
    ui->schoolmajor->clear();
    ui->workmajor->clear();
    ui->schoolname->clear();
    ui->workname->clear();
    ui->sheng->clear();
    ui->sheng->addItem("省");

    int row = ui->table->currentRow();
    //        if(rowIndex!=-1)
    //        {
    //            ui->pushButton_2->setEnabled(true);
    //            ui->pushButton_4->setEnabled(true);
    //        }
    int i=0;
    QVariant name = ui->table->item(row,i)->text();
    QString namestring=name.toString();
    ui->name->setText(namestring);
    i++;
    QVariant birth = ui->table->item(row,i)->text();
    QString birthstring=birth.toString();
    ui->birth->setText(birthstring);
    i++;
    QVariant id = ui->table->item(row,i)->text();
    QString idstring=id.toString();
    ui->id->setText(idstring);
    i++;
    QVariant sex = ui->table->item(row,i)->text();
    QString sexstring=sex.toString();
    ui->sex->setText(sexstring);
    i++;
    QVariant unit = ui->table->item(row,i)->text();
    QString unitstring=unit.toString();
    ui->unit->setText(unitstring);
    i++;
    QVariant zhiwu = ui->table->item(row,i)->text();
    QString zhiwustring=zhiwu.toString();
    ui->zhiwu->setText(zhiwustring);
    i++;
    QVariant minzu = ui->table->item(row,i)->text();
    QString minzustring=minzu.toString();
    ui->minzu->setText(minzustring);
    i++;
    QVariant rudang = ui->table->item(row,i)->text();
    QString rudangstring=rudang.toString();
    ui->rudang->setText(rudangstring);
    i++;
    QVariant joinwork = ui->table->item(row,i)->text();
    QString joinworkstring=joinwork.toString();
    ui->joinwork->setText(joinworkstring);
    i++;
    QVariant health = ui->table->item(row,i)->text();
    QString healthworkstring=health.toString();
    ui->health->setText(healthworkstring);
    i++;
    QVariant jiguan = ui->table->item(row,i)->text();
    QString jiguanstring=jiguan.toString();
    ui->jiguan->setText(jiguanstring);
    i++;
    QVariant politic = ui->table->item(row,i)->text();
    QString politicstring=politic.toString();
    ui->politic->setText(politicstring);
    i++;
    QVariant schoolmajor = ui->table->item(row,i)->text();
    QString schoolmajorstring=schoolmajor.toString();
    ui->schoolmajor->setText(schoolmajorstring);
    i++;
    QVariant workmajor = ui->table->item(row,i)->text();
    QString workmajorstring=workmajor.toString();
    ui->workmajor->setText(workmajorstring);


    i++;
    QVariant schoolname = ui->table->item(row,i)->text();
    QString schoolnamestring=schoolname.toString();
    ui->schoolname->setText(schoolnamestring);
    i++;
    QVariant workname = ui->table->item(row,i)->text();
    QString worknamestring=workname.toString();
    ui->workname->setText(worknamestring);




    QDate birth1;
    birth1=QDate::fromString(birthstring, "yyyy-MM-dd");
    ui->birth1->setDate(birth1);

    QDate rudang1;
    rudang1=QDate::fromString(rudangstring, "yyyy-MM-dd");
    ui->rudang1->setDate(rudang1);

    QDate joinwork1;
    joinwork1=QDate::fromString(joinworkstring, "yyyy-MM-dd");
    ui->joinwork1->setDate(joinwork1);

    QSqlQuery selectjiguan;
    selectjiguan.exec("select * from AdminAreaCode");
    while (selectjiguan.next()){
        QString alljiguanID=selectjiguan.value(0).toString();//籍贯代码
        QString alljiguan=selectjiguan.value(1).toString();

        QString ooxxxx=alljiguanID.mid(2,4);//后四位
        if(ooxxxx=="0000"){//后四位为0000，表示省份或自治区或直辖市
            ui->sheng->addItem(alljiguan);
        }
    }

    //设置ui->sheng,ui->shi,ui->qu
    //    QSqlQuery selectjiguan;
    //    selectjiguan.exec("select AdminAreaID form AdminAreaCode");
    //    while (selectjiguan.next()){
    //        QString all=selectjiguan.value(0).toString();//籍贯代码
    //        QString shengid=all.mid(1,4);
    //        qDebug()<<shengid;

    //    }
}

void renmianruanjian::on_pushButton_7_clicked()//把选中的数据显示后，保存回到table
{
    int row =ui->table->currentRow();
    QVariant unitdata1 = ui->table->item(row,4)->text();
    QString unitbefor=unitdata1.toString();
    //ui->radioButton->setAttribute(Qt::WA_TransparentForMouseEvents, true);
    QString name=ui->name->text();
    QString id= ui->id->text();
    QString unit=ui->unit->text();
    QString birth=ui->birth->text();
    QString sex=ui->sex->text();//combo
    QString zhiwu=ui->zhiwu->text();
    QString minzu=ui->minzu->text();
    QString rudang=ui->rudang->text();
    QString health=ui->health->text();
    QString jiguan=ui->jiguan->text();
    QString joinwork=ui->joinwork->text();
    QString politic=ui->politic->text();
    QString schoolmajor=ui->schoolmajor->text();
    QString workmajor=ui->workmajor->text();
    QString schoolname=ui->schoolname->text();
    QString workname=ui->workname->text();
    //    QDate birth1;
    //    birth1=QDate::fromString(birth, "yyyy-MM-dd");
    //    ui->birth1->setDate(birth1);
    //籍贯



    //性别需要通过combo修改
    //单位需要检测是否存在单位
    ui->table->setItem(row,4,new QTableWidgetItem("NULL"));
    QSqlQuery selectunit;
    selectunit.exec(QString("select unitid from unit where unitname='%1'").arg(unit));
    while(selectunit.next()){
        ui->table->setItem(row,4,new QTableWidgetItem(unit));
    }
    ui->table->setItem(row,0,new QTableWidgetItem(name));
    ui->table->setItem(row,1,new QTableWidgetItem(birth));
    ui->table->setItem(row,2,new QTableWidgetItem(id));
    ui->table->setItem(row,3,new QTableWidgetItem(sex));
    ui->table->setItem(row,5,new QTableWidgetItem(zhiwu));
    ui->table->setItem(row,6,new QTableWidgetItem(minzu));
    ui->table->setItem(row,7,new QTableWidgetItem(rudang));
    ui->table->setItem(row,8,new QTableWidgetItem(joinwork));
    ui->table->setItem(row,9,new QTableWidgetItem(health));
    ui->table->setItem(row,10,new QTableWidgetItem(jiguan));
    ui->table->setItem(row,11,new QTableWidgetItem(politic));
    ui->table->setItem(row,12,new QTableWidgetItem(schoolmajor));
    ui->table->setItem(row,13,new QTableWidgetItem(workmajor));
    ui->table->setItem(row,14,new QTableWidgetItem(schoolname));
    ui->table->setItem(row,15,new QTableWidgetItem(workname));

    QVariant unitdata2 = ui->table->item(row,4)->text();
    QString unitafter=unitdata2.toString();
    if(unitafter=="NULL"){
        QMessageBox::information(this, tr("提示信息"), tr("系统中没有")+unitbefor+("的单位信息"));
    }
}

void renmianruanjian::on_lineEdit_textChanged(const QString &arg1)//姓名排序
{
    int changerow=0;//第一行数据与选中的数据交换，然后changerow++
    int row =ui->table->rowCount();
    int lie=ui->table->columnCount();
    for(int i=0;i<row;i++){
        QVariant name = ui->table->item(i,0)->text();
        QString namestring=name.toString();
        if(namestring==arg1){
            QStringList selectRowLine, targetRowLine;//创建两个QStringList用于储存表格数据
            for (int x = 0; x < lie; x++)
            {
                selectRowLine.append(ui->table->item(i, x)->text());//获取文本，并添加进QStringLine
                targetRowLine.append(ui->table->item(changerow, x)->text());
            }
            for (int y = 0; y < lie; y++)
            {
                ui->table->setItem(i, y, new QTableWidgetItem(targetRowLine.at(y)));//设置文本
                ui->table->setItem(changerow, y, new QTableWidgetItem(selectRowLine.at(y)));
            }
            ui->table->selectRow(0);
            changerow++;
        }
    }
}

void renmianruanjian::on_lineEdit_2_textChanged(const QString &arg1)//单位排序
{
    int changerow=0;//第一行数据与选中的数据交换，然后changerow++
    int row =ui->table->rowCount();
    int lie=ui->table->columnCount();
    for(int i=0;i<row;i++){
        QVariant name = ui->table->item(i,4)->text();
        QString namestring=name.toString();
        if(namestring==arg1){
            QStringList selectRowLine, targetRowLine;//创建两个QStringList用于储存表格数据
            for (int x = 0; x < lie; x++)
            {
                selectRowLine.append(ui->table->item(i, x)->text());//获取文本，并添加进QStringLine
                targetRowLine.append(ui->table->item(changerow, x)->text());
            }
            for (int y = 0; y < lie; y++)
            {
                ui->table->setItem(i, y, new QTableWidgetItem(targetRowLine.at(y)));//设置文本
                ui->table->setItem(changerow, y, new QTableWidgetItem(selectRowLine.at(y)));
            }
            ui->table->selectRow(0);
            changerow++;
        }
    }
}

void renmianruanjian::on_sex1_currentIndexChanged(const QString &arg1)//性别修改
{
    if(arg1!="修改"){
        ui->sex->setText(arg1);
    }

}

void renmianruanjian::on_minzu1_currentIndexChanged(const QString &arg1)//民族修改
{
    if(arg1!="修改"){
        ui->minzu->setText(arg1);
    }
}

void renmianruanjian::on_birth1_dateChanged(const QDate &date)//生日时间修改
{
    QString birth;
    birth = date.toString("yyyy-MM-dd");
    ui->birth->setText(birth);
}

void renmianruanjian::on_table_clicked()
{
    ui->pushButton_2->setEnabled(true);
    ui->pushButton_4->setEnabled(true);
    ui->pushButton_5->setEnabled(true);
    //ui->scrollAreaWidgetContents->setAttribute(Qt::WA_TransparentForMouseEvents, true);
}

void renmianruanjian::on_rudang1_dateChanged(const QDate &date)
{
    QString rudang;
    rudang = date.toString("yyyy-MM-dd");
    ui->rudang->setText(rudang);
}

void renmianruanjian::on_joinwork1_dateChanged(const QDate &date)//修改参加工作时间
{
    QString joinwork;
    joinwork = date.toString("yyyy-MM-dd");
    ui->joinwork->setText(joinwork);
}

void renmianruanjian::on_politic1_currentIndexChanged(const QString &arg1)//修改政治面貌
{
    if(arg1!="修改"){
        ui->politic->setText(arg1);
    }
}

void renmianruanjian::on_pushButton_5_clicked()//更新选中的数据，与数据接受不同，如果检测到有数据，updata数据库
{
    //    QMessageBox::information(this, tr("提示信息"), tr("还没做好"));
    //先判断数据库中是否存在选中的数据
    int judge=0;//judge来判断执行什么操作：judge=0插入,judge=1更新
    int zhiwujudge=0;
    int edujudge=0;
    //定义查询
    QSqlQuery selecteduw;//查询学历表中是否存在相同的学历
    QSqlQuery selectzhiwu;//查询职务表中是否存在干部相同的职务信息,通过判断条件,限制插入语句,由于相同的数据,因此不需要更新数据
    QSqlQuery selectedus;//查询学历表中是否存在相同的学历
    QSqlQuery selectid;//查询是否存在干部数据
    QSqlQuery changeunit;//把单位名称改变为单位id
    QSqlQuery changeminzu;//把民族改为代码
    QSqlQuery changepolitic;//把政治面貌改为代码
    QSqlQuery changejiguan;
    //更新
    QSqlQuery upctest;//更新干部表
    QSqlQuery upzhiwu;//更新职务表
    QSqlQuery upedus;//更新全日制学历
    QSqlQuery upeduw;//更新在职学历

    //插入
    QSqlQuery insertctest;//插入干部表
    QSqlQuery insertzhiwu;//插入职务表
    QSqlQuery insertedus;//插入全日制学历
    QSqlQuery inserteduw;//插入在职学历
    //取出数据
    int row=ui->table->currentRow();
    int i=0;
    QVariant name = ui->table->item(row,i)->text();
    QString namedate=name.toString();
    i++;
    QVariant birth = ui->table->item(row,i)->text();
    QString birthdate=birth.toString();
    i++;
    QVariant id = ui->table->item(row,i)->text();
    QString iddate=id.toString();
    i++;
    QVariant sex = ui->table->item(row,i)->text();
    QString sexdate=sex.toString();
    if(sexdate=="男"){
        sexdate="1";
    }else if(sexdate=="女")
    {
        sexdate="0";
    }
    i++;
    QVariant unit = ui->table->item(row,i)->text();
    QString unitdate=unit.toString();//单位需要转换成单位的id
    changeunit.exec(QString("select unitid from unit where unitname='%1'").arg(unitdate));
    while(changeunit.next()){
        unitdate=changeunit.value(0).toString();
    }
    i++;
    //************************************
    QVariant zhiwu = ui->table->item(row,i)->text();
    QString zhiwudate=zhiwu.toString();
    //    QString zhiwudate="职务";
    //******************************
    i++;
    QVariant minzu = ui->table->item(row,i)->text();//民族换为代码
    QString minzudate=minzu.toString();
    QString minzucode;
    changeminzu.exec(QString("select NationID from NationCode where Nation ='%1'").arg(minzudate));
    while(changeminzu.next()){
        minzucode=changeminzu.value(0).toString();
    }
    i++;
    QVariant rudnag = ui->table->item(row,i)->text();
    QString rudnagdate=rudnag.toString();
    i++;
    QVariant joinwork = ui->table->item(row,i)->text();
    QString joinworkdate=joinwork.toString();
    i++;
    QVariant health = ui->table->item(row,i)->text();
    QString healthdate=health.toString();
    i++;
    QVariant jiguan = ui->table->item(row,i)->text();
    QString jiguandate=jiguan.toString();
    changejiguan.exec(QString("select AdminAreaID from AdminAreaCode where AdminArea='%1'").arg(jiguandate));
    while(changejiguan.next()){
        jiguandate=changejiguan.value(0).toString();
    }

    i++;
    QVariant politic = ui->table->item(row,i)->text();
    QString politicdate=politic.toString();//政治面面貌需要换成代码
    changepolitic.exec(QString("select PoliticID from PoliticCode where Politic='%1'").arg(politicdate));
    while(changepolitic.next()){
        politicdate=changepolitic.value(0).toString();
    }
    i++;
    QVariant schooledu = ui->table->item(row,i)->text();
    QString schooledudate=schooledu.toString();//全日专业判断条件是1
    i++;
    QVariant workedu = ui->table->item(row,i)->text();
    QString workedudate=workedu.toString();//在职专业判断条件是0

    i++;
    QVariant schoolname = ui->table->item(row,i)->text();
    QString schoolnamedate=schoolname.toString();//全日院校判断条件是1
    i++;
    QVariant workname = ui->table->item(row,i)->text();
    QString worknamedate=workname.toString();//在职院校判断条件是0




    //查询条件
    selectid.exec(QString("select IDNum from Ctest where IDNum='%1'").arg(iddate));
    //更新条件
    // query.prepare("update faceimage set StrName=?,Sijian=? where Faceid=?");
    // query.addBindValue(this->ui->XGuserName->text());
    // query.addBindValue(this->ui->XGidNumber->text());
    // query.addBindValue(Faceid);
    // query.exec();
    upctest.prepare("update Ctest set CadreName=?,Birth=?,Sex=?,UnitID=?,NationID=?,PartyTime=?,JoinWorkTime=?,Health=?,NativePlace=?,Zhiwu=?,PoliticID=?,SchoolEdu=?,WorkEdu=? where IDNum=?");//更新干部表
    upzhiwu.prepare(QString("update Post set PostName=? where IDNum='%1'and QualifyType='%2'").arg(iddate).arg(0));//更新职务表
    upedus.prepare(QString("update Edu set Major=?,School=? where IDNum='%1'and WorkORSchool='%2'").arg(iddate).arg(1));//更新全日制学历
    upeduw.prepare(QString("update Edu set Major=?,School=? where IDNum='%1'and WorkORSchool='%2'").arg(iddate).arg(0));//更新在职学历

    //插入条件
    inserteduw.prepare("INSERT INTO Edu (EduID,IDNum,Major,WorkORSchool,School)" "VALUES(0,?,?,?,?)");
    insertedus.prepare("INSERT INTO Edu (EduID,IDNum,Major,WorkORSchool,School)" "VALUES(0,?,?,?,?)");
    insertctest.prepare("INSERT INTO Ctest(IDNum,CadreName,Birth,Sex,UnitID,Zhiwu,NationID,PartyTime,JoinWorkTime,Health,NativePlace,PoliticID,SchoolEdu,WorkEdu)" "VALUES(:IDNum,:CadreName,:Birth,:Sex,:UnitID,:Zhiwu,:NationID,:PartyTime,:JoinWorkTime,:Health,:NativePlace,:PoliticID,:SchoolEdu,:WorkEdu)");
    insertzhiwu.prepare("INSERT INTO Post(PostName,IDNum,QualifyType)""VALUES(:name,:id,:type)");
    //    //若有这个数据：对数据库更新数据!!!!!!!!!!这里是Ctest表，以后都要换成CadreInfo
    while(selectid.next()){
        QMessageBox::information(this, tr("提示信息"), tr("更新")+namedate+("的数据"));
        judge=1;
        //姓名  生日   性别   单位   民族  入党时间   参加工作时间   健康    籍贯   职务   政治面貌   全日制   在职
        upctest.addBindValue(namedate);
        upctest.addBindValue(birthdate);
        upctest.addBindValue(sexdate);
        upctest.addBindValue(unitdate);
        upctest.addBindValue(minzucode);
        upctest.addBindValue(rudnagdate);
        upctest.addBindValue(joinworkdate);
        upctest.addBindValue(healthdate);
        upctest.addBindValue(jiguandate);
        upctest.addBindValue(zhiwudate);
        upctest.addBindValue(politicdate);
        upctest.addBindValue(schooledudate);
        upctest.addBindValue(workedudate);
        upctest.addBindValue(iddate);
        upctest.exec();
    }
    //    //若没有这个数据：对数据库插入数据
    if(judge==0){
        QMessageBox::information(this, tr("提示信息"), tr("增加")+namedate+("的数据"));
        insertctest.bindValue(":IDNum",iddate);//this->ui->iddate->text()
        insertctest.bindValue(":CadreName",namedate);//this->ui->iddate->text()
        insertctest.bindValue(":Birth",birthdate);//this->ui->iddate->text()
        insertctest.bindValue(":Sex",sexdate);//this->ui->iddate->text()
        insertctest.bindValue(":UnitID",unitdate);//this->ui->iddate->text()
        insertctest.bindValue(":Zhiwu",zhiwudate);//this->ui->iddate->text()
        insertctest.bindValue(":NationID",minzucode);//this->ui->iddate->text()
        insertctest.bindValue(":PartyTime",rudnagdate);//this->ui->iddate->text()
        insertctest.bindValue(":JoinWorkTime",joinworkdate);//this->ui->iddate->text()
        insertctest.bindValue(":Health",healthdate);//this->ui->iddate->text()
        insertctest.bindValue(":NativePlace",jiguandate);//this->ui->iddate->text()
        insertctest.bindValue(":PoliticID",politicdate);//this->ui->iddate->text()
        insertctest.bindValue(":SchoolEdu",schooledudate);//this->ui->iddate->text()
        insertctest.bindValue(":WorkEdu",workedudate);//this->ui->iddate->text()
    }
    selectzhiwu.exec(QString("select PostID from Post where IDNum='%1' and QualifyType='%2'").arg(iddate).arg(0));
    while(selectzhiwu.next())
    {
        zhiwujudge=1;
        QMessageBox::information(this, tr("提示信息"), tr("更新")+namedate+("的职务信息"));
        upzhiwu.addBindValue(zhiwudate);
        upzhiwu.exec();
        //QString postid=selectzhiwu.value(0).toString();
        //qDebug()<<postid;
    }
    if(zhiwujudge==0){
        QMessageBox::information(this, tr("提示信息"), tr("新增")+namedate+("的职务信息"));
        //insertzhiwu.bindValue(":post",1);//this->ui->iddate->text()
        insertzhiwu.bindValue(":name",zhiwudate);//this->ui->iddate->text()
        insertzhiwu.bindValue(":id",iddate);//this->ui->iddate->text()
        insertzhiwu.bindValue(":type",0);//this->ui->iddate->text()//0现任职务
        //    query.bindValue(":ata",12);//this->ui->iddate->text()//一定要有冒号：
        //    query.bindValue(":StrName",jiguanstring);
        //    query.bindValue(":Sijian",this->ui->dateEdit->text());
        //    query.bindValue(":FaceData",11);//this->ui->iddate->text()
        //    query.bindValue(":StrName",this->ui->namedate->text());
        //    query.bindValue(":Sijian",this->ui->numdate->text());
        insertzhiwu.exec();
    }
    //在职=0
    selecteduw.exec(QString("select EduID from Edu where IDNum='%1'and WorkORSchool='%2'").arg(iddate).arg(0));
    while(selecteduw.next()){
        edujudge=1;
        QMessageBox::information(this, tr("提示信息"), tr("更新")+namedate+("的在职专业信息"));
        upeduw.addBindValue(workedudate);
        upeduw.addBindValue(worknamedate);

        upeduw.exec();
    }
    if(edujudge==0){
        QMessageBox::information(this, tr("提示信息"), tr("新增")+namedate+("的在职专业信息"));
        //            insertedu.prepare("insert into Edu(Major,WorkORSchool,IDNum) values(:Major,:WorkORSchool,:IDNum)");
        inserteduw.bindValue(0,iddate);
        inserteduw.bindValue(1,workedudate);
        inserteduw.bindValue(2,0);
        inserteduw.bindValue(3,worknamedate);
        inserteduw.exec();
    }
    edujudge=0;
    //全日=1
    selectedus.exec(QString("select EduID from Edu where IDNum='%1'and Major='%2'and WorkORSchool='%3'").arg(iddate).arg(schooledudate).arg(1));
    while(selectedus.next()){
        edujudge=1;
        QMessageBox::information(this, tr("提示信息"), tr("更新")+namedate+("的全日专业信息"));
        upedus.addBindValue(schooledudate);
        upedus.addBindValue(schoolnamedate);

        upedus.exec();
    }
    if(edujudge==0){
        //            insertedu.prepare("insert into Edu(Major,WorkORSchool,IDNum) values(:Major,:WorkORSchool,:IDNum)");
        QMessageBox::information(this, tr("提示信息"), tr("新增")+namedate+("的全日专业信息"));
        insertedus.bindValue(0,iddate);
        insertedus.bindValue(1,schooledudate);
        insertedus.bindValue(2,1);
        insertedus.bindValue(3,schoolnamedate);
        insertedus.exec();
    }
    insertctest.exec();
}

void renmianruanjian::on_pushButton_3_clicked()//更新全部的数据
{
    //    QMessageBox::information(this, tr("提示信息"), tr("还没做好"));
    //先判断数据库中是否存在选中的数据
    int judge=0;//judge来判断执行什么操作：judge=0插入,judge=1更新
    int zhiwujudge=0;
    int edujudge=0;
    //定义查询
    QSqlQuery selecteduw;//查询学历表中是否存在相同的学历
    QSqlQuery selectzhiwu;//查询职务表中是否存在干部相同的职务信息,通过判断条件,限制插入语句,由于相同的数据,因此不需要更新数据
    QSqlQuery selectedus;//查询学历表中是否存在相同的学历
    QSqlQuery selectid;//查询是否存在干部数据
    QSqlQuery changeunit;//把单位名称改变为单位id
    QSqlQuery changeminzu;//把民族改为代码
    QSqlQuery changepolitic;//把政治面貌改为代码
    //更新
    QSqlQuery upctest;//更新干部表
    QSqlQuery upzhiwu;//更新职务表
    QSqlQuery upedus;//更新全日制学历
    QSqlQuery upeduw;//更新在职学历
    //插入
    QSqlQuery insertctest;//插入干部表
    QSqlQuery insertzhiwu;//插入职务表
    QSqlQuery insertedus;//插入全日制学历
    QSqlQuery inserteduw;//插入在职学历
    //取出数据
    int rownum=ui->table->rowCount();
    int row;
    for(row=0;row<rownum;row++){
        int i=0;
        QVariant name = ui->table->item(row,i)->text();
        QString namedate=name.toString();
        i++;
        QVariant birth = ui->table->item(row,i)->text();
        QString birthdate=birth.toString();
        i++;
        QVariant id = ui->table->item(row,i)->text();
        QString iddate=id.toString();
        i++;
        QVariant sex = ui->table->item(row,i)->text();
        QString sexdate=sex.toString();
        if(sexdate=="男"){
            sexdate="1";
        }else if(sexdate=="女")
        {
            sexdate="0";
        }
        i++;
        QVariant unit = ui->table->item(row,i)->text();
        QString unitdate=unit.toString();//单位需要转换成单位的id
        changeunit.exec(QString("select unitid from unit where unitname='%1'").arg(unitdate));
        while(changeunit.next()){
            unitdate=changeunit.value(0).toString();
        }
        i++;
        //************************************
        QVariant zhiwu = ui->table->item(row,i)->text();
        QString zhiwudate=zhiwu.toString();
        //    QString zhiwudate="职务";
        //******************************
        i++;
        QVariant minzu = ui->table->item(row,i)->text();//民族换为代码
        QString minzudate=minzu.toString();
        QString minzucode;
        changeminzu.exec(QString("select NationID from NationCode where Nation ='%1'").arg(minzudate));
        while(changeminzu.next()){
            minzucode=changeminzu.value(0).toString();
        }
        i++;
        QVariant rudnag = ui->table->item(row,i)->text();
        QString rudnagdate=rudnag.toString();
        i++;
        QVariant joinwork = ui->table->item(row,i)->text();
        QString joinworkdate=joinwork.toString();
        i++;
        QVariant health = ui->table->item(row,i)->text();
        QString healthdate=health.toString();
        i++;
        QVariant jiguan = ui->table->item(row,i)->text();
        QString jiguandate=jiguan.toString();
        i++;
        QVariant politic = ui->table->item(row,i)->text();
        QString politicdate=politic.toString();//政治面面貌需要换成代码
        changepolitic.exec(QString("select PoliticID from PoliticCode where Politic='%1'").arg(politicdate));
        while(changepolitic.next()){
            politicdate=changepolitic.value(0).toString();
        }
        i++;
        QVariant schooledu = ui->table->item(row,i)->text();
        QString schooledudate=schooledu.toString();//全日专业判断条件是1
        i++;
        QVariant workedu = ui->table->item(row,i)->text();
        QString workedudate=workedu.toString();//在职专业判断条件是0

        i++;
        QVariant schoolname = ui->table->item(row,i)->text();
        QString schoolnamedate=schoolname.toString();//全日专业判断条件是1
        i++;
        QVariant workname = ui->table->item(row,i)->text();
        QString worknamedate=workname.toString();//在职专业判断条件是0


        //查询条件
        selectid.exec(QString("select IDNum from Ctest where IDNum='%1'").arg(iddate));
        //更新条件
        // query.prepare("update faceimage set StrName=?,Sijian=? where Faceid=?");
        // query.addBindValue(this->ui->XGuserName->text());
        // query.addBindValue(this->ui->XGidNumber->text());
        // query.addBindValue(Faceid);
        // query.exec();
        upctest.prepare("update Ctest set CadreName=?,Birth=?,Sex=?,UnitID=?,NationID=?,PartyTime=?,JoinWorkTime=?,Health=?,NativePlace=?,Zhiwu=?,PoliticID=?,SchoolEdu=?,WorkEdu=? where IDNum=?");//更新干部表
        upzhiwu.prepare(QString("update Post set PostName=? where IDNum='%1'and QualifyType='%2'").arg(iddate).arg(0));//更新职务表
        upedus.prepare(QString("update Edu set Major=?,School=? where IDNum='%1'and WorkORSchool='%2'").arg(iddate).arg(1));//更新全日制学历
        upeduw.prepare(QString("update Edu set Major=?,School=? where IDNum='%1'and WorkORSchool='%2'").arg(iddate).arg(0));//更新在职学历

        //插入条件
        inserteduw.prepare("INSERT INTO Edu (EduID,IDNum,Major,WorkORSchool,School)" "VALUES(0,?,?,?,?)");
        insertedus.prepare("INSERT INTO Edu (EduID,IDNum,Major,WorkORSchool,School)" "VALUES(0,?,?,?,?)");
        insertctest.prepare("INSERT INTO Ctest(IDNum,CadreName,Birth,Sex,UnitID,Zhiwu,NationID,PartyTime,JoinWorkTime,Health,NativePlace,PoliticID,SchoolEdu,WorkEdu)" "VALUES(:IDNum,:CadreName,:Birth,:Sex,:UnitID,:Zhiwu,:NationID,:PartyTime,:JoinWorkTime,:Health,:NativePlace,:PoliticID,:SchoolEdu,:WorkEdu)");
        insertzhiwu.prepare("INSERT INTO Post(PostName,IDNum,QualifyType)""VALUES(:name,:id,:type)");

        //    //若有这个数据：对数据库更新数据!!!!!!!!!!这里是Ctest表，以后都要换成CadreInfo
        while(selectid.next()){
            QMessageBox::information(this, tr("提示信息"), tr("更新")+namedate+("的数据"));
            judge=1;
            //姓名  生日   性别   单位   民族  入党时间   参加工作时间   健康    籍贯   职务   政治面貌   全日制   在职
            upctest.addBindValue(namedate);
            upctest.addBindValue(birthdate);
            upctest.addBindValue(sexdate);
            upctest.addBindValue(unitdate);
            upctest.addBindValue(minzucode);
            upctest.addBindValue(rudnagdate);
            upctest.addBindValue(joinworkdate);
            upctest.addBindValue(healthdate);
            upctest.addBindValue(jiguandate);
            upctest.addBindValue(zhiwudate);
            upctest.addBindValue(politicdate);
            upctest.addBindValue(schooledudate);
            upctest.addBindValue(workedudate);
            upctest.addBindValue(iddate);
            upctest.exec();
        }
        //    //若没有这个数据：对数据库插入数据
        if(judge==0){
            QMessageBox::information(this, tr("提示信息"), tr("增加")+namedate+("的数据"));
            insertctest.bindValue(":IDNum",iddate);//this->ui->iddate->text()
            insertctest.bindValue(":CadreName",namedate);//this->ui->iddate->text()
            insertctest.bindValue(":Birth",birthdate);//this->ui->iddate->text()
            insertctest.bindValue(":Sex",sexdate);//this->ui->iddate->text()
            insertctest.bindValue(":UnitID",unitdate);//this->ui->iddate->text()
            insertctest.bindValue(":Zhiwu",zhiwudate);//this->ui->iddate->text()
            insertctest.bindValue(":NationID",minzucode);//this->ui->iddate->text()
            insertctest.bindValue(":PartyTime",rudnagdate);//this->ui->iddate->text()
            insertctest.bindValue(":JoinWorkTime",joinworkdate);//this->ui->iddate->text()
            insertctest.bindValue(":Health",healthdate);//this->ui->iddate->text()
            insertctest.bindValue(":NativePlace",jiguandate);//this->ui->iddate->text()
            insertctest.bindValue(":PoliticID",politicdate);//this->ui->iddate->text()
            insertctest.bindValue(":SchoolEdu",schooledudate);//this->ui->iddate->text()
            insertctest.bindValue(":WorkEdu",workedudate);//this->ui->iddate->text()
        }

        selectzhiwu.exec(QString("select PostID from Post where IDNum='%1' and QualifyType='%2'").arg(iddate).arg(0));
        while(selectzhiwu.next())
        {
            zhiwujudge=1;
            QMessageBox::information(this, tr("提示信息"), tr("更新")+namedate+("的职务信息"));
            upzhiwu.addBindValue(zhiwudate);
            upzhiwu.exec();
            //QString postid=selectzhiwu.value(0).toString();
            //qDebug()<<postid;
        }
        if(zhiwujudge==0){
            QMessageBox::information(this, tr("提示信息"), tr("新增")+namedate+("的职务信息"));
            //insertzhiwu.bindValue(":post",1);//this->ui->iddate->text()
            insertzhiwu.bindValue(":name",zhiwudate);//this->ui->iddate->text()
            insertzhiwu.bindValue(":id",iddate);//this->ui->iddate->text()
            insertzhiwu.bindValue(":type",0);//this->ui->iddate->text()//0现任职务
            //    query.bindValue(":ata",12);//this->ui->iddate->text()//一定要有冒号：
            //    query.bindValue(":StrName",jiguanstring);
            //    query.bindValue(":Sijian",this->ui->dateEdit->text());
            //    query.bindValue(":FaceData",11);//this->ui->iddate->text()
            //    query.bindValue(":StrName",this->ui->namedate->text());
            //    query.bindValue(":Sijian",this->ui->numdate->text());
            insertzhiwu.exec();
        }
        //在职=0
        selecteduw.exec(QString("select EduID from Edu where IDNum='%1'and WorkORSchool='%2'").arg(iddate).arg(0));
        while(selecteduw.next()){
            edujudge=1;
            QMessageBox::information(this, tr("提示信息"), tr("更新")+namedate+("的在职院校以及专业信息"));
            upeduw.addBindValue(workedudate);
            upeduw.addBindValue(worknamedate);

            upeduw.exec();
        }
        if(edujudge==0){
            QMessageBox::information(this, tr("提示信息"), tr("新增")+namedate+("的在职院校以及专业信息"));
            //            insertedu.prepare("insert into Edu(Major,WorkORSchool,IDNum) values(:Major,:WorkORSchool,:IDNum)");
            inserteduw.bindValue(0,iddate);
            inserteduw.bindValue(1,workedudate);
            inserteduw.bindValue(2,0);
            inserteduw.bindValue(3,worknamedate);
            inserteduw.exec();
        }
        edujudge=0;
        //全日=1
        selectedus.exec(QString("select EduID from Edu where IDNum='%1'and Major='%2'and WorkORSchool='%3'").arg(iddate).arg(schooledudate).arg(1));
        while(selectedus.next()){
            edujudge=1;
            QMessageBox::information(this, tr("提示信息"), tr("更新")+namedate+("的全日院校以及专业信息"));
            upedus.addBindValue(schooledudate);
            upedus.addBindValue(schoolnamedate);
            upedus.exec();
        }
        if(edujudge==0){
            //            insertedu.prepare("insert into Edu(Major,WorkORSchool,IDNum) values(:Major,:WorkORSchool,:IDNum)");
            QMessageBox::information(this, tr("提示信息"), tr("新增")+namedate+("的全日院校以及专业信息"));
            insertedus.bindValue(0,iddate);
            insertedus.bindValue(1,schooledudate);
            insertedus.bindValue(2,1);
            insertedus.bindValue(3,schoolnamedate);
            insertedus.exec();
        }
        insertctest.exec();
    }
}

void renmianruanjian::on_pushButton_2_clicked()//导入照片
{

    QMessageBox::information(this, tr("提示信息"), tr("还没想好该怎么做"));
}



void renmianruanjian::on_qu_currentIndexChanged(const QString &arg1)//写入籍贯信息
{
    QString sheng=ui->sheng->currentText();
    ui->jiguan->setText(sheng+arg1);
}

